package com.sh4.controller.xgfs;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sh4.entity.XgfsBook;
import com.sh4.service.IfilesService;
import com.sh4.service.XgfsBookService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@RestController
@RequestMapping(value = "xgfs/book")
public class BookController {
	
	@Autowired
	XgfsBookService xgfsBookService;
	
	@Autowired IfilesService ifilesService;

	@RequestMapping(value = "/list" , method = RequestMethod.POST)
	public Map<String , Object> list(@RequestParam int pageNo , @RequestParam int pageSize , @RequestBody JSONObject reqBody){
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			result.put("dataList", xgfsBookService.list(pageSize, pageNo, reqBody));
			result.put("total", xgfsBookService.countXgfsBook(reqBody));
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
	}
	
	@RequestMapping(value = "/" , method = RequestMethod.POST)
	public Map<String, Object> save(@RequestBody JSONObject reqBody) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			String fileIds = reqBody.getString("fileIds");
			reqBody.discard("fileIds");
			XgfsBook xb = (XgfsBook) JSONObject.toBean(reqBody, XgfsBook.class);
			xgfsBookService.save(xb);
			ifilesService.updateIfiles(fileIds, xb.getId());
			
			result.put("data", xb);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
		
	}
	
	@RequestMapping(value = "/cancle" , method = RequestMethod.POST)
	public Map<String, Object> cancle(@RequestBody XgfsBook xb) {
		
		Map<String, Object> result = new HashMap<String,Object>();
		int status = 200;
		String msg = "提交成功";
		try {
			
			xgfsBookService.cancle(xb);
			result.put("data", xb);
		} catch (Exception e) {
			status = 300;
			msg = "内部错误";
		}
		result.put("status", status); 
		result.put("msg", msg);
		
		return result;
		
	}
	
}
